<template>
	<view class="login-container">
		
		<view class="content">
			<view class="logo-box">
				<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
				<text class="system-name">大学生预警系统</text>
			</view>
			
			<view class="form-box">
				<view class="input-item">
					<input 
						type="text" 
						v-model="loginText.sSid" 
						placeholder="请输入学号" 
						placeholder-class="placeholder"
					/>
				</view>
				
				<view class="input-item">
					<input 
						type="password" 
						v-model="loginText.sPassword" 
						placeholder="请输入密码" 
						placeholder-class="placeholder"
					/>
				</view>
				
				<button class="login-btn" @click="stulogin">登录</button>
				
				<view class="forgot-pwd">
					<text @click="handleForgot">忘记密码？联系管理员</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { reactive, toRefs } from 'vue';
import {login} from '@/api/student.js'
import { initWebSocket } from '@/utils/websocket.js';

export default {
    setup() {
        const state = reactive({
			loginText:{
				sSid:'202213008407',
				sPassword:'123456'
			}
		})
		function stulogin(){
			if(state.loginText.sSid == '' || state.loginText.sPassword){
				uni.showToast({
				title: '请输入账号或密码',
				icon: 'error',
				duration: 2000
				});
			}
			login(state.loginText).then(res=>{
				if(res.code == 1){
					uni.showToast({
					title: '登录成功',
					icon: 'success',
					duration: 2000
					});
					uni.setStorageSync("token",res.data.token);
					uni.setStorageSync("student",res.data.sstudent);
					initWebSocket();
					uni.switchTab({
						url:"/pages/index/index"
					});
				}else{
					uni.showToast({
					title: res.msg,
					icon: 'error',
					duration: 2000
					});
				}
			})
		}
		return {
			...toRefs(state),
			stulogin
		}
	}
}
</script>

<style lang="scss">
page {
	background-color: #119790;
}

.login-container {
	min-height: 100%;
	display: flex;
	flex-direction: column;
	
	.header {
		height: 88rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #119790;
		
		.title {
			font-size: 32rpx;
			color: #333;
			font-weight: 500;
		}
	}
	
	.content {
		flex: 1;
		padding: 60rpx 40rpx;
		
		.logo-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-bottom: 60rpx;
			
			.logo {
				width: 120rpx;
				height: 120rpx;
				margin-bottom: 20rpx;
			}
			
			.system-name {
				font-size: 36rpx;
				color: #333;
				font-weight: bold;
			}
		}
		
		.form-box {
			background: #fff;
			border-radius: 12rpx;
			padding: 40rpx 30rpx;
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
			
			.input-item {
				height: 90rpx;
				border-bottom: 1rpx solid #eee;
				margin-bottom: 20rpx;
				
				input {
					height: 100%;
					font-size: 28rpx;
					color: #333;
				}
				
				.placeholder {
					color: #999;
				}
			}
			
			.login-btn {
				margin-top: 50rpx;
				height: 90rpx;
				line-height: 90rpx;
				background: #2979ff;
				color: #fff;
				font-size: 32rpx;
				border-radius: 45rpx;
				
				&:active {
					background: darken(#2979ff, 5%);
				}
			}
			
			.forgot-pwd {
				margin-top: 30rpx;
				text-align: center;
				
				text {
					font-size: 24rpx;
					color: #666;
				}
			}
		}
	}
}
</style>